using System.Collections.Generic;
using System.IO;
using Microsoft.Build.Evaluation;
using NUnit.Framework;
using System.Linq;

namespace projectfile.tests
{
    [TestFixture]
    public class ProjectParsenTests
    {
        private ProjectParser sut;
        private IEnumerable<string> ebcXmlFilenames;
        private string generatedEbcCsFilename;

        [SetUp]
        public void Setup() {
            ProjectCollection.GlobalProjectCollection.UnloadAllProjects();
            sut = new ProjectParser();
            sut.EbcXmlFilenames += x => ebcXmlFilenames = x;
            sut.GeneratedEbcCsFilename += x => generatedEbcCsFilename = x;
        }

        [Test]
        public void Ebc_Xml_Dateien_des_Projekts_werden_erkannt() {
            const string dateiname = @"..\..\projectfile.tests.csproj";
            var path = Path.GetDirectoryName(Path.GetFullPath(dateiname));
            var project = new Project(dateiname);

            sut.GetEbcXmlFilenames(project);
            
            Assert.That(ebcXmlFilenames.ToArray(), Is.EquivalentTo(new [] {
                Path.Combine(path, @"Testdata\Ebcs\a.ebc.xml"), 
                Path.Combine(path, @"Testdata\Ebcs\b.ebc.xml")
            }));
        }

        [Test]
        public void Generated_ebc_cs_file_is_found_in_subdirectory_of_the_project() {
            const string dateiname = @"..\..\projectfile.tests.csproj";
            var path = Path.GetDirectoryName(Path.GetFullPath(dateiname));
            var project = new Project(dateiname);

            sut.GetGeneratedEbcCsFilename(project);

            Assert.That(generatedEbcCsFilename, Is.EqualTo(Path.Combine(path, @"Testdata\Generated\generated.ebc.cs")));
        }

        [Test]
        public void Generated_ebc_cs_filename_is_at_project_root_directory() {
            const string dateiname = @"..\..\..\empty\empty.csproj";
            var path = Path.GetDirectoryName(Path.GetFullPath(dateiname));
            var project = new Project(dateiname);

            sut.GetGeneratedEbcCsFilename(project);

            Assert.That(generatedEbcCsFilename, Is.EqualTo(Path.Combine(path, @"generated.ebc.cs")));
        }
    }
}